package utils

import (
	"encoding/json"
	"fmt"
	"net"
)

type Response struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

func NewResponse(success bool, message string) *Response {
	return &Response{
		Success: success,
		Message: message,
	}
}

func SendResponse(conn net.Conn, resp *Response) error {
	fmt.Println("Sending response:", resp)
	data, err := json.Marshal(resp)
	if err != nil {
		return err
	}
	_, err = conn.Write(append(data, '\n'))
	return err
}

func SendSuccess(conn net.Conn, message string) error {
	return SendResponse(conn, &Response{
		Success: true,
		Message: message,
	})
}

func SendError(conn net.Conn, message string) error {
	return SendResponse(conn, &Response{
		Success: false,
		Message: message,
	})
}
